<?php

namespace backend\controllers;

use backend\server\RentingTypeImageServer;
use common\logic\RentingTypeImageLogic;
use Yii;
use common\models\RentingTypeImageModel;
use yii\data\ActiveDataProvider;
use yii\web\NotFoundHttpException;

/**
 * HouseTypeImageController implements the CRUD actions for HouseTypeImageModel model.
 */
class RentingTypeImageController extends BaseController
{

    /**
     * Lists all HouseTypeImageModel models.
     *
     * @return mixed
     */
    public function actionIndex($house_id)
    {
        $dataProvider = new ActiveDataProvider([
            'query' => RentingTypeImageLogic::instance()->getQuery(['house_id' => $house_id], [], [], ['id' => SORT_ASC]),
        ]);
        $dataProvider->sort = false;

        return $this->render('index', [
            'dataProvider' => $dataProvider,
            'house_id' => $house_id
        ]);
    }

    /**
     * Displays a single HouseTypeImageModel model.
     *
     * @param integer $id
     *
     * @return mixed
     */
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new HouseTypeImageModel model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     *
     * @return mixed
     */
    public function actionCreate($house_id)
    {
        $model = new RentingTypeImageModel();
        $model->house_id = $house_id;
        if ($model->load(Yii::$app->request->post()) && RentingTypeImageServer::instance()->save($model)) {
            return $this->redirect(['index', 'house_id' => $model->house_id]);
        } else {
            return $this->render('create', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Updates an existing HouseTypeImageModel model.
     * If update is successful, the browser will be redirected to the 'view' page.
     *
     * @param integer $id
     *
     * @return mixed
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        $post = Yii::$app->request->post();
        unset($post['RentingTypeImageModel']['img_path']);
        if ($model->load($post) && RentingTypeImageServer::instance()->save($model)) {
            return $this->redirect(['index', 'house_id' => $model->house_id]);
        } else {
            return $this->render('update', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Deletes an existing HouseTypeImageModel model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     *
     * @param integer $id
     *
     * @return mixed
     */
    public function actionDelete($id)
    {
        $model = $this->findModel($id);
        $model->delete();

        return $this->redirect(['index', 'house_id' => $model->house_id]);
    }

    /**
     * Finds the HouseTypeImageModel model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     *
     * @param integer $id
     *
     * @return RentingTypeImageModel the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function  findModel($id)
    {
        if (($model = RentingTypeImageModel::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
}
